package br.com.q7solucoes.q7webservicej.ws;

import br.com.q7solucoes.commons.wordpress.WPPost;
import br.com.q7solucoes.commons.wordpress.WPPostDAO;
import br.com.q7solucoes.lazerbrwebservicej.control.WebXmlController;
import java.util.Date;
import java.util.List;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

/**
 * Classe que representa o WebService.
 *
 * @author Elenilson Vieira - elenilson[at]q7solucoes.com.br
 * @since 27 de fevereiro de 2012
 * @version 1.0
 * @see www.q7solucoes.com.br
 */
@WebService(serviceName = "Q7WebServiceJ")
public class Q7WebServiceJ {

    /**
     * Web service operation
     */
    @WebMethod(operationName = "addPost")
    public boolean addPost(@WebParam(name = "post") final WPPost post) {
        WPPostDAO wPPostDAO = getWPPostDAO();
        
        try{
            wPPostDAO.add(post);
            return true;
        }catch(Exception ex){
            return false;
        }
    }

    private WPPostDAO getWPPostDAO() {
        String mysqlDatabaseHost = WebXmlController.getInstance().getMysqlDataBaseHost();
        String mysqlDatabase = WebXmlController.getInstance().getMysqlDataBase();
        String mysqlDatabaseCode = WebXmlController.getInstance().getMysqlDataBaseCode();
        String mysqlDatabaseUser = WebXmlController.getInstance().getMysqlDataBaseUser();
        String mysqlDatabasePassword = WebXmlController.getInstance().getMysqlDataBasePassword();
        
        try{
            return new WPPostDAO(mysqlDatabase, mysqlDatabaseHost, mysqlDatabaseUser, mysqlDatabasePassword, mysqlDatabaseCode);
        }catch(Exception ex){
            return null;
        }
    }

    /**
     * Web service operation
     */
    @WebMethod(operationName = "getPosts")
    public List<WPPost> getPosts(@WebParam(name = "beginDate") final Date beginDate, @WebParam(name = "endDate") final Date endDate) {
        WPPostDAO dao = getWPPostDAO();
        
        try{
            return dao.get(beginDate, endDate);
        }catch(Exception ex){
            return null;
        }
    }
}
